
function $(e){
    return document.querySelector(e)
}
// 获取元素
var btn = $(".btn")
var cons = $(".cons")
var login = $(".login")
var registered=$('.registered')
var text1=$('.text1')
var text2=$('.text2')
var img =$('.g')
var pwd =$('.pwd')
var inp=$('.inp')
var yc =$('.yc')
var log =$('.login-btn')
var reg =$('.reg-btn')
var account =$('.account')
var pas =$('.pas')
var remember =$('#remember')
var chek =$('.chek')
var codeBox =$('#codeBox')
var c=$('.c')
var btnC=$('.btn_c')
var rand=$('.rand')
console.log(rand);

//	记录当前时间戳 7天免登录
var logintimes = Date.parse(new Date());
console.log(logintimes)
window.localStorage.setItem("logintimes",logintimes)

//绑定事件
var flag =0;
img.onclick = function (){
    console.log(1);
    if(flag == 0){
img.src = 'http://localhost:2205/img/open.png';
pwd.type = 'text';
// 点击之后flag一定要发生变化
// 赋值操作
flag =1
 }else {
img.src = 'http://localhost:2205/img/close.png';
pwd.type = 'password';
flag = 0;
}
}

inp.onfocus = function () {
   if( this.value == '手机号/用户名/邮箱' ){
    this.value = '';
   }
    this.style.color = '#333'
}
inp.onblur = function () {
    var rg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
    if (rg.test(this.value)) {
        yc.className = 'dui'
        yc.innerHTML = '正确'
    }else{
    yc.className = 'wrong'
    yc.innerHTML = '错误'
   }
    this.style.color = '#000'
}
btn.onclick = function(){
    cons.style.display ='block'
    login.style.display ='block'
}
// 选项卡
login.onclick =function() {
    text1.style.display ='block'
    text2.style.display ='none'
    login.className = 'login'
     registered.className = 'registered'
    
}
registered.onclick =function() {
    text1.style.display ='none'
    text2.style.display ='block'
     login.className = 'registered'
    registered.className = 'login'
}

 //页面刷新加载出来如果存到值,那么会出现在登录页面上
 if(localStorage.getItem('username') ){
    account.value = localStorage.getItem('username')
    pwd.value = localStorage.getItem('pass')
    remember.checked = true

}

remember.onclick=function(){
    console.log(1);
    if(this.checked){
        localStorage.setItem('username', account.value)
        localStorage.setItem('pass', pwd.value)
        
    }else{
        localStorage.removeItem('username')
        localStorage.removeItem('pass')
    }
}

btn.onclick=function(){
    // 为空或者密码不正确不能登录
    if(inp.value == '' || pwd.value == ''){
        alert("密码或者账号不正确，请重新输入。")
        return false
    }
    if(!chek.checked){
        alert("请勾选协议")
        return false
    }
 
}
// 随机数验证
function createNumber(){
    var number =
    '0123456789abcdefghijklmopqrseuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM'
   var codeTxet='';
    for(let i = 1; i<=4;i++){
        var oor =Math.floor(Math.random()*62)
        var add = number.charAt(oor)
        codeTxet +=add;
} 
 codeBox.innerHTML = codeTxet;
 return codeTxet
}
var ran = createNumber()
console.log(ran);
codeBox.onclick = createNumber;
c.onblur = function () {
    console.log(ran);
    if( this.value == ran ){
        rand.className = 'dui'
        rand.innerHTML = '正确'
    }else{
    rand.className = 'wrong'
    rand.innerHTML = '错误'
   }
    this.style.color = '#000'
 }
 btnC.onclick = function(){
    if(rand.className == 'dui'){
        location.href ='http://localhost:2205/html/login.html'
    }else{
        alert('验证码不正确，请重新输入')
        return false
    }
 }